package com.qffx.moviebase.model;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.qffx.moviebase.bean.ADEntity;
import com.qffx.moviebase.model.impl.IADM;
import com.qffx.moviebase.utils.Url;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ADModel {
    private List<ADEntity.SplashBean> list;
    private Context context;
    private IADM iadm;
    public ADModel(Context context, IADM iadm) {
        this.context = context;
        this.iadm = iadm;
        list=new ArrayList<>();

    }
    public void loadData() {

        new Thread() {
            @Override
            public void run() {
                super.run();
                RequestQueue requestQueue = Volley.newRequestQueue(context);
                StringRequest stringRequest = new StringRequest(StringRequest.Method.GET, Url.AD_URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject object = new JSONObject(response);
                            JSONObject splash = object.getJSONObject("splash");
                            ADEntity.SplashBean splashBean = JSON.parseObject(splash.toString(), ADEntity.SplashBean.class);
                            list.add(splashBean);
                            iadm.sendData(list);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, null);
                requestQueue.add(stringRequest);

            }
        }.start();



    }

    public ADEntity.SplashBean getSplashBean(int index) {
        return list.get(index);

    }


}
